//
//  SettleListVC.swift
//  kxf
//  结算管理
//  Created by Mac on 2024/3/19.
//

import Foundation
import MCToast
import MJRefresh

class SettleListVC : WHBaseViewVC {
    
    var pageNum = 1
    var pageSize = 5
    
    var dataSourceArray = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        sendDataQueryRequest()
        
//        self.presentAlertVcNoAction(title: "此页面尚未调试列表数据的参数", viewController: self)
    }
    lazy var topVm : SettleListTopVM = {
        let vm = SettleListTopVM.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(10), width: 0, height: 0))
        
        return vm
    }()
    lazy var tableView : UITableView = {
        let vi = UITableView.init(frame: CGRect.init(x: 0, y: self.topVm.frame.maxY+kFitWidth(10), width: SCREEN_WIDHT, height: SCREEN_HEIGHT-(self.topVm.frame.maxY+kFitWidth(10))), style: .plain)
        vi.delegate = self
        vi.dataSource = self
        vi.separatorStyle = .none
        vi.backgroundColor = .COLOR_LIGHT_GREY
        
        vi.register(SettleListTableViewCell.classForCoder(), forCellReuseIdentifier: "SettleListTableViewCell")
        
        if #available(iOS 11.0, *) {
            vi.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        
        vi.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.pageNum = 1
            self.sendDataQueryRequest()
        })

        vi.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {
            self.pageNum += 1
            self.sendDataQueryRequest()
        })
        
        return vi
    }()
    lazy var emptyView : EmptyViewWithBtn = {
        let emptyViewT = EmptyViewWithBtn.init(frame: CGRect.zero)
        emptyViewT.isHidden = true
        emptyViewT.contentLabel.text = "还没有记录~"
        
        return emptyViewT
    }()
    
}

extension SettleListVC{
    func initUI() {
        initNavi(titleStr: "结算管理")
        
        view.backgroundColor = .COLOR_LIGHT_GREY
        view.addSubview(topVm)
        
        view.addSubview(tableView)
        tableView.addSubview(emptyView)
        emptyView.center = CGPoint.init(x: SCREEN_WIDHT*0.5, y: self.tableView.frame.height*0.5)
    }
}

extension SettleListVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        emptyView.isHidden = dataSourceArray.count > 0 ? true : false
        return dataSourceArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let dict = self.dataSourceArray[indexPath.row] as? NSDictionary ?? [:]
        let cell = tableView.dequeueReusableCell(withIdentifier: "SettleListTableViewCell") as! SettleListTableViewCell
         cell.updateUI(dict: dict)
         
         return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return kFitWidth(94)
    }
}


extension SettleListVC{
    func sendDataQueryRequest() {
        MCToast.mc_loading()
        let param = ["pageNum":"\(pageNum)","pageSize":"\(pageSize)"]
        WHNetworkUtil.shareManager().POST(urlString: "\(URL_settle_query)?type=0", parameters: param as [String:AnyObject],isNeedToast: true,vc:self) { responseObject in
            DLLog(message: "\(responseObject)")
            
            self.tableView.mj_footer!.endRefreshing()
            self.tableView.mj_header!.endRefreshing()
            
            let dataObj = responseObject["data"]as? NSDictionary ?? [:]
            self.topVm.updateUI(dict: dataObj["obj"]as? NSDictionary ?? [:])
            
            if self.pageNum == 1{
                self.dataSourceArray.removeAllObjects()
            }
            
            let dataArr = dataObj["list"]as? NSArray ?? []
            // 提示没有更多数据
            if dataArr.count < self.pageSize {
                self.tableView.mj_footer!.endRefreshingWithNoMoreData()
            }
            
            self.dataSourceArray.addObjects(from: dataArr as! [Any])
            self.tableView.reloadData()
        }
    }
}
